package com.zbh.dao;

import cn.hutool.crypto.SecureUtil;
import com.zbh.domain.User;
import com.zbh.util.DbUtilHikari;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 对用户表的操作：注册用户、删除用户、查询用户、修改用户
 * @author zbh
 * @version 1.0
 * @classname UserDao
 * @date 2022/12/31 10:35
 */
public class UserDao {

    /**
     * 查询所有的用户
     * @return 包含所有用户对象的集合
     */
    public List<User> selectAllUser() {
        // 创建一个空集合准备放用户对象
        List<User> userList = new ArrayList<>();

        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        conn = DbUtilHikari.getConnection();
        String sql = "SELECT * FROM users";

        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            // 把 rs 里的数据转移到 userList 里
            while (rs.next()) {
                User user = new User();
                // 这个 getInt 的参数 得和数据库字段名一样
                user.setUserid(rs.getInt("userid"));
                user.setUsername(rs.getString("username"));
                user.setUserpsw(rs.getString("userpsw"));
                userList.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DbUtilHikari.close(rs, pstmt, conn);
        }

        return userList;
    }

    /**
     * 修改用户信息
     * @param userid 用户编号
     * @param uname 用户名
     * @param upsw 密码
     * @return 修改成功返回 1，否则返回 0
     */
    public int updateUser(int userid, String uname, String upsw) {
        int flag = 0;

        Connection conn = null;
        PreparedStatement pstmt = null;

        conn = DbUtilHikari.getConnection();
        String sql = "UPDATE users SET username = ?, userpsw = ? WHERE userid = ?";

        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, uname);
            pstmt.setString(2, SecureUtil.md5(upsw));
            pstmt.setInt(3, userid);
            flag = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DbUtilHikari.close(pstmt, conn);
        }

        return flag;
    }

    /**
     * 按用户编号删除用户
     * @param userid 用户编号
     * @return 删除成功返回 1，否则返回 0
     */
    public int deleteUser(int userid) {
        int flag = 0;

        Connection conn = null;
        PreparedStatement pstmt = null;

        conn = DbUtilHikari.getConnection();

        String sql = "DELETE FROM users WHERE userid = ?";

        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, userid);
            flag = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DbUtilHikari.close(pstmt, conn);
        }

        return flag;
    }

    /**
     * 用户登录
     * @param uname
     * @param upsw
     * @return
     */
    public boolean login(String uname, String upsw) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        boolean flag = false;

        conn = DbUtilHikari.getConnection();
        String sql = "SELECT * FROM users WHERE username = ? AND userpsw = ?";
        try {
            pstmt = conn.prepareStatement(sql);

            pstmt.setString(1, uname);
            pstmt.setString(2, SecureUtil.md5(upsw));

            rs = pstmt.executeQuery() ;

            // 判断结果集是否为空
            if (rs.next()) {
                flag = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DbUtilHikari.close(rs, pstmt, conn);
        }

        return flag;
    }

    /**
     * 用户注册
     * @param uname 用户名
     * @param upsw 密码
     * @return 注册成功返回 1，否则返回 0
     */
    public int insertUser(String uname, String upsw) {
        // 1. 声明一些变量备用
        // 代表数据库连接
        Connection conn = null;

        // 预编译对象，用 pstmt 执行 sql 语句
        PreparedStatement pstmt = null;

        // 注册成功与否的标志，如果是 0 注册失败；如果是 1 注册成功
        int flag = 0;

        // 2. 获取连接
        conn = DbUtilHikari.getConnection();

        // 3. 定义并准备 SQL 语句
        String sql = "INSERT INTO users(username, userpsw) VALUES(?, ?)";

        try {
            pstmt = conn.prepareStatement(sql);

            // 4. 形参赋值
            pstmt.setString(1, uname);
            pstmt.setString(2, SecureUtil.md5(upsw));

            // 5. 执行 SQL 语句
            flag = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 6. 关闭数据库资源
            DbUtilHikari.close(pstmt, conn);
        }

        return flag;
    }
}